//
//  BCSettingCenterController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCSettingCenterController.h"

//cell
#import "BCSettingCenterBaseCell.h"
//section footer
#import "BCSettingCenterFooterView.h"

//cellModel
#import "SettingCenterCellModel.h"

//vc
#import "BCLanguageSettingController.h"//语言设置
#import "BCConversionController.h"//货币兑换
#import "BCVersionInfoController.h"//版本信息

//
#import "BCUserCenterResponse.h"
//
#import "BCSettingCacheManager.h"
#import "BCAppVersionManager.h"

@interface BCSettingCenterController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *settingTable;


/**
 *settingSource 以section为单位 具体看代码
 */
@property (nonatomic, strong) NSMutableArray *settingSource;

@end

@implementation BCSettingCenterController

- (NSMutableArray *)settingSource {
    if (!_settingSource) {
        _settingSource = [NSMutableArray array];
        //是否显示正常UI
        BOOL normalUI = [BCAppVersionManager sharedManager].showNormalUI;
        //语言、缓存
        NSMutableArray *sectionSource_l = [NSMutableArray array];
        [sectionSource_l addObject:[SettingCenterCellModel language]];
        [sectionSource_l addObject:[SettingCenterCellModel clearCache]];
        //
        [_settingSource addObject:sectionSource_l];
        //
        NSMutableArray *sectionSource_c = [NSMutableArray array];
        [sectionSource_c addObject:[SettingCenterCellModel conversion]];
        //
        [_settingSource addObject:sectionSource_c];
        //
        if (normalUI) {
            NSMutableArray *sectionSource_v = [NSMutableArray array];
            [sectionSource_v addObject:[SettingCenterCellModel applicationInfomation]];
            //
            [_settingSource addObject:sectionSource_v];
        }
        
    }
    return _settingSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.title = self.centerData.flow.setting?:BCLocalizedKey(@"uc_setting_center");
    //
    [self setupSubviews];
}

- (void)setupSubviews {
    self.settingTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.settingTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.settingTable.backgroundColor = [UIColor separatorColor];
    self.settingTable.dataSource = self;
    self.settingTable.delegate = self;
    [self.view addSubview:self.settingTable];
    [self.settingTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    //
    [self.settingTable registerClass:BCSettingCenterFooterView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(BCSettingCenterFooterView.class)];
    NSMutableArray *cells = [SettingCenterCellModel registerClasses];
    for (NSString *cellName in cells) {
        [self.settingTable registerClass:NSClassFromString(cellName) forCellReuseIdentifier:cellName];
    }
    //
    UIView *tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 25)];
    [self.settingTable setTableHeaderView:tableHeader];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.settingSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section<self.settingSource.count) {
        NSArray *sectionSource = self.settingSource[section];
        if (sectionSource&&[sectionSource isKindOfClass:NSArray.class]) {
            return sectionSource.count;
        }
    }
    return 0;
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section<self.settingSource.count) {
        NSArray *sectionSource = self.settingSource[indexPath.section];
        if (sectionSource&&[sectionSource isKindOfClass:NSArray.class]) {
            SettingCenterCellModel *cellModel = sectionSource[indexPath.row];
            BCSettingCenterBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellModel.cellClass)];
            cell.cellModel = cellModel;
            return cell;
        }
    }
    BCSettingCenterBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCSettingCenterBaseCell.class)];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BCSettingCenterBaseCell defaultHeight];
}
//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    BCSettingCenterFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(BCSettingCenterFooterView.class)];
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15.0f;
}
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section<self.settingSource.count) {
        NSArray *sectionSource = self.settingSource[indexPath.section];
        if (sectionSource&&[sectionSource isKindOfClass:NSArray.class]) {
            SettingCenterCellModel *cellModel = sectionSource[indexPath.row];
            switch (cellModel.cellType) {
                case SettingCenterCellTypeLanguage:
                {
                    [self toLanguageSettingAction];
                }
                    break;
                case SettingCenterCellTypeClearCacha:
                {
                    [self toClearCacheAction];
                }
                    break;
                case SettingCenterCellTypeConversion: {
                    [self toConversionCurrencyAction];
                }
                    break;
                case SettingCenterCellTypeApplicationInfo:
                {//版本信息
                    [self toApplicationInfomationAction];
                }
                    break;
                default:
                    break;
            }
        }
    }
}


#pragma mark - action
/**
 清除缓存
 */
- (void)toClearCacheAction {
    if ([BCSettingCacheManager sharedCache].needClear) {
        UIView *tipsView = [UIApplication sharedApplication].keyWindow;
        [QMUITips showLoading:BCLocalizedKey(@"Waiting clear") inView:tipsView];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            WEAK_SELF
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [[BCSettingCacheManager sharedCache] clearCacheComplete:^{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [QMUITips hideAllTipsInView:tipsView];
                        [wself.settingTable reloadData];
                        //
                        NSString *success = BCLocalizedKey(@"Clear success");
                        [QMUITips showSucceed:success inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
                    });
                }];
            });
        });
    }else {
        NSString *success = BCLocalizedKey(@"Clear success");
        [QMUITips showSucceed:success inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
    }
}
/**
 去语言设置
 */
- (void)toLanguageSettingAction {
    BCLanguageSettingController *languageSetting = [BCLanguageSettingController new];
    [self.navigationController presentViewController:languageSetting animated:true completion:nil];
}
/**
 去兑换
 */
- (void)toConversionCurrencyAction {
    BCConversionController *conversion = [BCConversionController new];
    [self.navigationController pushViewController:conversion animated:true];
}
/**
 去版本信息
 */
- (void)toApplicationInfomationAction {
    BCVersionInfoController *versionInfo = [BCVersionInfoController new];
    [self.navigationController pushViewController:versionInfo animated:true];
}

@end
